package cn.com.majiang.controller;

import cn.com.majiang.domain.Manager;
import cn.com.majiang.enums.ResultCode;
import cn.com.majiang.param.AddActualCardParam;
import cn.com.majiang.param.ManagerParam;
import cn.com.majiang.result.BaseResult;
import cn.com.majiang.service.IManagerService;
import cn.com.majiang.service.ITechargerecordService;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * Created by jiangyanxin on 2017/5/28.
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/proxy")
public class ManagerController {
    Logger logger = LoggerFactory.getLogger(ManagerController.class);
    @Autowired
    IManagerService managerService;
    @Autowired
    ITechargerecordService techargerecordService;

    /**
     * 搜索代理列表
     *
     * @throws Exception
     */
    @RequestMapping(value = "/searchManagerList", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public BaseResult searchManagerList(String str, HttpServletRequest request) throws Exception {
        long curr_time = System.currentTimeMillis();
        logger.info("搜索代理商列表入参:{}", str);
        if (StringUtils.isBlank(str)) {
            return BaseResult.build(ResultCode.NULL_REQUESTPARAM);
        }
        ManagerParam args = JSON.parseObject(str, ManagerParam.class);
        HttpSession session = request.getSession();
        Integer id = (Integer)session.getAttribute("id");
        if(id != 1){
            args.setManagerUpId(id);
        }
        Map<String, Object> result = managerService.searchManagerList(args);
        logger.info("搜索代理列表耗时：" + (System.currentTimeMillis() - curr_time));
        return BaseResult.build(ResultCode.REQUEST_SUCCESS.getCode(), ResultCode.REQUEST_SUCCESS.getMessage(), result);
    }

    /**
     * 修改代理商状态(删除代理商)
     *
     * @param str
     */
    @RequestMapping("/updateManagerStatus")
    @ResponseBody
    public BaseResult updateManagerStatus(String str) {
        logger.info("更新代理商状态参数" + str);
        if (StringUtils.isBlank(str)) {
            return BaseResult.build(ResultCode.NULL_REQUESTPARAM);
        }
        Manager args = JSON.parseObject(str, Manager.class);
        if (managerService.editManagerStatus(args)) {
            return BaseResult.build(ResultCode.REQUEST_SUCCESS);
        } else {
            return BaseResult.build(ResultCode.REQUEST_FAIL);
        }
    }

    /**
     * 添加代理商
     *
     * @param str
     */
    @RequestMapping("/addManager")
    @ResponseBody
    public BaseResult addManager(String str,HttpServletRequest request) {
        logger.info("添加代理商参数" + str);
        if (StringUtils.isBlank(str)) {
            return BaseResult.build(ResultCode.NULL_REQUESTPARAM);
        }
        HttpSession session = request.getSession();
        Integer id = (Integer) session.getAttribute("id");
        if(id == null){
            logger.info("获取登录id为空");
            return BaseResult.build(ResultCode.REQUEST_PARAM_ERROR);
        }
        Manager args = JSON.parseObject(str, Manager.class);
        if (StringUtils.isBlank(args.getName()) || StringUtils.isBlank(args.getPassword()) || StringUtils.isBlank(args.getTelephone())) {
            return BaseResult.build(ResultCode.NULL_REQUESTPARAM);
        }
        args.setManagerUpId(id);
        args.setCreateUser(session.getAttribute("name")+"");
        Manager selManager = managerService.selectManagerByUsername(args.getName());
        if(selManager != null){
            logger.info("代理商"+args.getCreateUser()+"已存在");
            return BaseResult.build(ResultCode.MANAGER_EXIST);
        }
        if (managerService.saveManager(args)) {
            return BaseResult.build(ResultCode.REQUEST_SUCCESS);
        } else {
            return BaseResult.build(ResultCode.REQUEST_FAIL);
        }
    }


    /**
     * 给代理商充值
     *
     * @param str
     */
    @RequestMapping("/addActualCard")
    @ResponseBody
    public BaseResult addActualCard(String str, HttpServletRequest request) {
        logger.info("给代理商充值参数" + str);
        if (StringUtils.isBlank(str)) {
            return BaseResult.build(ResultCode.NULL_REQUESTPARAM);
        }
        AddActualCardParam param = JSON.parseObject(str, AddActualCardParam.class);
        if (!StringUtils.isNumeric(param.getPlayCardNum())) {
            return BaseResult.build(ResultCode.REQUEST_PARAM_ERROR);
        }
        if(param.getId() == null || StringUtils.isBlank(param.getChargetype())){
            return BaseResult.build(ResultCode.NULL_REQUESTPARAM);
        }
        int payCardNum = Integer.parseInt(param.getPlayCardNum());
        HttpSession session = request.getSession();
        Manager manager = (Manager) session.getAttribute("manager");
        if (manager != null) {
            if (managerService.updateManagerRoomCard(param.getId(), manager, payCardNum)) {
                techargerecordService.saveManagerTechargerecord(param.getUserid(), manager.getName(), payCardNum,param.getChargetype());
                return BaseResult.build(ResultCode.REQUEST_SUCCESS);
            }
        }
        return BaseResult.build(ResultCode.REQUEST_FAIL);
    }

}
